package cn.edu.nju.software.cdiy.action.user.all;

import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import cn.edu.nju.software.cdiy.bean.Hardware;
import cn.edu.nju.software.cdiy.bean.Hcomment;
import cn.edu.nju.software.cdiy.service.HcommentService;
import cn.edu.nju.software.cdiy.service.Service;
import cn.edu.nju.software.cdiy.util.BusinessConstant;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class HardwareCommentAction<T extends Hardware> extends ActionSupport implements SessionAware {

	private static String S_HARDWARE;
	private static String HARDWARE_TYPE;
	private Map<String, Object> session;
	private Service<T> service;
	private HcommentService hcommentService;
	private int id;
	private int commentType;
	private List<Integer> statisticsList;
	private List<Hcomment> commentList;
	private int pageNum = 1;

	public static void setS_HARDWARE(String s_HARDWARE) {
		S_HARDWARE = s_HARDWARE;
	}

	public static void setHARDWARE_TYPE(String hARDWARE_TYPE) {
		HARDWARE_TYPE = hARDWARE_TYPE;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public void setService(Service<T> service) {
		this.service = service;
	}

	public void setHcommentService(HcommentService hcommentService) {
		this.hcommentService = hcommentService;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getCommentType() {
		return commentType;
	}

	public void setCommentType(int commentType) {
		this.commentType = commentType;
	}

	public List<Integer> getStatisticsList() {
		return statisticsList;
	}

	public List<Hcomment> getCommentList() {
		return commentList;
	}

	public int getPageNum() {
		return pageNum;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String execute() throws Exception {
		T sessionHardware = (T) session.get(S_HARDWARE);
		if (sessionHardware==null || sessionHardware.getId()!=id) {
			T hardware = service.getOneById(id);
			if (hardware == null) {
				return ERROR;
			}
			session.put(S_HARDWARE, hardware);
		}
		if (commentType == BusinessConstant.COMMENT_USER) {
			statisticsList = hcommentService.getUserNumberList(id, HARDWARE_TYPE);
			commentList = hcommentService.getUserHCommentPage(id, HARDWARE_TYPE, pageNum);
		} else {
			statisticsList = hcommentService.getExpertNumberList(id, HARDWARE_TYPE);
			commentList = hcommentService.getExpertHCommentPage(id, HARDWARE_TYPE, pageNum);
		}
		return SUCCESS;
	}

}
